package com.wxxymakers.cklabrecruit.controller;

import com.wxxymakers.cklabrecruit.entity.EmailRecord;
import com.wxxymakers.cklabrecruit.entity.EmailReturn;
import com.wxxymakers.cklabrecruit.entity.RegisterInfo;
import com.wxxymakers.cklabrecruit.service.EmailReturnService;
import com.wxxymakers.cklabrecruit.service.EmailTemplateService;
import com.wxxymakers.cklabrecruit.service.RegisterInfoService;
import com.wxxymakers.cklabrecruit.utils.EmailUtils;
import com.wxxymakers.cklabrecruit.utils.RestResponse;
import com.wxxymakers.cklabrecruit.utils.Utils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @AUTHOR soft
 * @DATE 2018/11/30 21:26
 * @DESCRIBE 邮件反馈控制器
 */
@Controller
@RequestMapping("/email/return")
public class EmailReturnController {

    private final EmailReturnService service;
    private final EmailTemplateService templateService;
    private final RegisterInfoService registerInfoService;
    private final EmailUtils emailUtils;

    @Autowired
    public EmailReturnController(EmailReturnService service, EmailTemplateService templateService,
                                 RegisterInfoService registerInfoService, EmailUtils emailUtils) {
        this.service = service;
        this.templateService = templateService;
        this.registerInfoService = registerInfoService;
        this.emailUtils = emailUtils;
    }

    @PostMapping("/page")
    @RequiresRoles({"admin"})
    @ResponseBody
    public RestResponse page(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "7", required = false) Integer size,
                             EmailReturn emailReturn) {
        Page<EmailReturn> page = service.page(pageNum, size, emailReturn);
        if (null != page) {
            return RestResponse.ok(page);
        }
        return RestResponse.fail("暂无反馈数据！");
    }

    @PostMapping("/resend")
    @RequiresRoles({"admin"})
    @ResponseBody
    public RestResponse resend(EmailReturn emailReturn, HttpServletRequest request) {
        String url = request.getRequestURL().toString();
        String host = url.substring(0, url.indexOf("/", 8));

        if (emailReturn != null) {
            EmailRecord record = templateService.getRecord(emailReturn.getRecordId());
            RegisterInfo info = registerInfoService.getOne(emailReturn.getRegId());
            if (record != null && info != null) {
                String content = record.getContent();
                String returnUrl = host + "/email/return/t/" + Utils.tos(info.getId(), record.getId());
                String realEmail = emailUtils.getRealEmail(content, info, returnUrl);
                emailUtils.send(record.getTitle(), "皖西学院创客实验室[提示邮件]",
                        info.getEmail(), realEmail);
                service.addReSendNum(emailReturn.getId()); // 添加提示次数
                return RestResponse.ok("重发成功！");
            }
        }
        return RestResponse.fail("无法重新发送，缺失必要信息");
    }

    /**
     * 用户回执添加
     * @param info 用户信息 包涵报名用户id和邮件记录id
     */
    @GetMapping("/t/{info}")
    public String return0(@PathVariable String info) {
        String[] froms = Utils.froms(info);
        if (froms != null && froms.length >= 2) {
            EmailReturn emailReturn = new EmailReturn();
            emailReturn.setRegId(Integer.parseInt(froms[0]));
            emailReturn.setRecordId(Integer.parseInt(froms[1]));
            emailReturn.setStatus(true);
            if (service.change(emailReturn)) {
                return "return/success";
            }
        }
        return "return/fail";
    }
}
